/**
 * @Title: http.js
 * @Description: http
 * @author: Fanchao
 * @date: 2020/8/31 2:00 下午
 * @version V1.0
 */
import axios from 'axios';
import { message } from 'antd';
import storageUtils from 'utils/storage';

axios.defaults.baseURL = process.env.baseURL;
axios.interceptors.request.use(
    (config) => {
        const accessToken = storageUtils.getToken();
        if (accessToken) {
            config.headers['token'] = accessToken.token;
            config.headers['secret'] = accessToken.secret;
        }

        return config;
    },
    (error) => {
        // 请求错误
        return Promise.reject(error);
    },
);
class httpRequest {
    baseOptions(params, method = 'GET') {
        const accessToken = storageUtils.getToken();

        let { url, data = {} } = params;
        data.accessToken = accessToken;
        let contentType = 'application/json';
        contentType = params.contentType || contentType;
        let responseType = 'json';
        responseType = params.responseType || responseType;
        const option = {
            url,
            data,
            method,
            header: {
                'content-type': contentType,
            },
            responseType,
        };

        return new Promise((resolve, reject) => {
            let promise = axios.request(option);
            promise.then(response => {
                const { status, message: msg, headers } = response;
                if (status === 200 || status === 201) {
                    const contentDisposition = headers['content-disposition'];

                    if (contentDisposition) {
                        const filenames = decodeURI(contentDisposition.substring(contentDisposition.indexOf('filename=') + 9, contentDisposition.length));
                        const filename = decodeURI(contentDisposition.substring(contentDisposition.indexOf('filename=') + 10, contentDisposition.length - 1));

                        // console.log(filename);
                        // console.log(filenames);

                        // let blob = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" });
                        // let aElement = document.createElement("a");
                        // aElement.href = window.URL.createObjectURL(blob);
                        // aElement.download = filename;
                        // aElement.click();
                        // aElement.remove();


                        const resObj = {
                            file: response.data,
                            filename,
                        };
                        resolve(resObj);
                    } else {
                        resolve(response.data);
                    }
                } else {
                    message.error(msg);
                }
                // 返回正常数据
            }).catch(error => { // 失败 不调用reject 而是提示异常信息
                message.error(`请求出错了:${error}`);
            });

        });
    }

    get(url, data = '') {
        let option = { url, data };
        return this.baseOptions(option);
    }

    post(url, data, contentType, responseType) {
        let params = { url, data, contentType, responseType };
        return this.baseOptions(params, 'POST');
    }

    put(url, data = '') {
        let option = { url, data };
        return this.baseOptions(option, 'PUT');
    }

    delete(url, data = '') {
        let option = { url, data };
        return this.baseOptions(option, 'DELETE');
    }

}

export default new httpRequest();
